package com.dmwork.service.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.work.entity.WorkType;
import com.dmwork.service.work.entity.vo.WorkTypeBriefInfoVo;
import com.dmwork.service.work.entity.vo.WorkTypeDetailInfoVo;
import com.dmwork.service.work.mapper.WorkTypeDescriptionMapper;
import com.dmwork.service.work.mapper.WorkTypeMapper;
import com.dmwork.service.work.service.WorkTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-03-14
 */
@Service
public class WorkTypeServiceImpl extends ServiceImpl<WorkTypeMapper, WorkType> implements WorkTypeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private WorkTypeDescriptionMapper workTypeDescriptionMapper;

    @Cacheable(value = "briefInfoList", key = " #root.methodName ")
    @Override
    public List<WorkTypeBriefInfoVo> getBriefInfoList() {
        List<WorkTypeBriefInfoVo> workTypeBriefInfoVoList = new ArrayList<>();
        List<WorkType> workTypeList = baseMapper.selectList(new QueryWrapper<>());
        for (WorkType workType : workTypeList) {
            WorkTypeBriefInfoVo workTypeBriefInfoVo = new WorkTypeBriefInfoVo();
            BeanUtils.copyProperties(workType, workTypeBriefInfoVo);
            workTypeBriefInfoVoList.add(workTypeBriefInfoVo);
        }
        return workTypeBriefInfoVoList;
    }

    @Cacheable(value = "workTypeDetailInfoVoList", key = "#root.methodName")
    @Override
    public List<WorkTypeDetailInfoVo> getDetailInfoList() {
        return baseMapper.getWorkTypeDetailInfoList();
    }


    @Override
    public WorkTypeDetailInfoVo getDetailInfo(String roles, String typeId) {
        switch (roles) {
            case "front":
                String key = "WorkTypeServiceImpl.getDetailInfo";
                String hashKey = typeId;
                if(redisTemplate.opsForHash().hasKey(key, hashKey)) {
                    return (WorkTypeDetailInfoVo) redisTemplate.opsForHash().get(key, hashKey);
                }else {
                    WorkTypeDetailInfoVo workTypeDetailInfoVo = baseMapper.getWorkTypeDetailInfo(typeId);
                    redisTemplate.opsForHash().put(key, hashKey, workTypeDetailInfoVo);
                    return workTypeDetailInfoVo;
                }
            case "admin":
                return baseMapper.getWorkTypeDetailInfo(typeId);
            default:
                throw new DmworkException(ResultCodeEnum.SELECT_WORK_TYPE_ERROR);
        }

    }
}
